<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Model\Feedback;
use App\Model\FeedbackImg;
use Session;

class FeedbackController extends BaseController
{
    /**
     * 问题反馈视图
     */
    public function index()
    {
        return view('Home/feedback/index');
    }

    /**
     * 问题反馈处理
     */
    public function add(Request $request)
    {
        $data = $request->all();
        $user = session('home_user');
        $data['feebback']['user_name'] =$user['user_name'];
        $data['feebback']['phone'] =$user['phone'];
        $data['feebback']['uid'] =$user['uid'];
        if(!$data['feebback']['content']){
            return ['code'=>0,'data'=>'请填写反馈内容'];
        }
        $Rows = Feedback::create($data['feebback']);
        $fid = empty($data['feebback']['id'])?$Rows->id : $data['feebback']['id'];
        $ImgName = empty($data['ImgName'])?0:$data['ImgName'];
        $rico = $this->updateImg($fid,$ImgName);
        if ($Rows || $rico){
            return ['code'=>1,'data'=>'提交成功'];
         } else {
             return ['code'=>0,'data'=>'提交失败'];
         }
    }
    /**
     * 问题反馈处理
     */
    public function find()
    {
        $user = session('home_user');
        $Rows = Feedback::where(['uid' =>$user['uid']])->orderBy('id','desc')->get();
        if ($Rows){
            return ['code'=>1,'data'=>$Rows];
        } else {
            return ['code'=>0,'data'=>'提交失败'];
        }
    }
    public function problem()
    {
        $user = session('home_user');
    }

    /**
     * 截图图路径
     * @param int $fid
     * @param array $data
     * @return bool
     * @throws \Exception
     */
    public function updateImg($fid = 0,$data = array())
    {
        if ($fid > 0) {
            FeedbackImg::where('fid',$fid)->delete();
        }
        $rico = '';
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $rico = FeedbackImg::create(['fid'=>$fid,'feedback_img'=>$v]);
            }
        }
        if ($rico) {
            return true;
        } else {
            return false;
        }
    }

}